﻿using System;
using RayDen.Library.Core;
using RayDen.RayEngine.Core.Interface;
using RayDen.RayEngine.Core.Types;

namespace RayDen.RayEngine.Samplers
{
    public class DadeUniformSampler : ISampler
    {
        public static int PreviewPasses = 16;
        public static int IterationPerPass = 16;
        public static int MagicValue2 = 16;
        public int Width { get; protected set; }
        public int Height { get; protected set; }

        public int screenWidth, screenHeight, screenStartLine;
        int currentSampleScreenX, currentSampleScreenY, currentSubSampleIndex;
        public int pass;
        public bool lowLatency;
        public int valuesPerSample;

        private IRandomProvider rnd;

        public int tIdx;



        public DadeUniformSampler()
        {
            rnd =
                new FastRandom();
            //new HaltonSequence();
            TotalSamples = 0L;
        }

        int ScreenHeight
        {
            get { return screenStartLine / tIdx; }
        }

        public Sample GetSample(Sample prevSample)
        {
            TotalSamples++;
            Sample sample = prevSample??new Sample(this);
            int scrX, scrY;

            if (!lowLatency || (pass >= MagicValue2))
            {
                // In order to improve ray coherency
                int stepX = currentSubSampleIndex % 4;
                int stepY = currentSubSampleIndex / 4;

                scrX = currentSampleScreenX;
                scrY = currentSampleScreenY;

                currentSubSampleIndex++;
                if (currentSubSampleIndex == (IterationPerPass))
                {
                    currentSubSampleIndex = 0;
                    currentSampleScreenX++;
                    if (currentSampleScreenX >= screenWidth)
                    {
                        currentSampleScreenX = 0;
                        currentSampleScreenY++;

                        if (currentSampleScreenY >= screenHeight)
                        {
                            currentSampleScreenY = Math.Min(0, screenStartLine - 1);
                            pass += IterationPerPass;
                        }
                    }
                }

                float r1 = (stepX + ((float)rnd.NextDouble())) / 4f - 0.5f;
                float r2 = (stepY + ((float)rnd.NextDouble())) / 4f - 0.5f;
                sample.imageX = scrX + r1;
                sample.imageY = scrY + r2;
                //sample.imageY += screenStartLine;

                //sample.Init(this,pass, valuesPerSample, false);
            }
            else if (pass >= PreviewPasses)
            {
                scrX = currentSampleScreenX;
                scrY = currentSampleScreenY;

                currentSampleScreenX++;
                if (currentSampleScreenX >= screenWidth)
                {
                    currentSampleScreenX = 0;
                    currentSampleScreenY++;

                    if (currentSampleScreenY >= screenHeight)
                    {
                        currentSampleScreenY = Math.Min(0, screenStartLine - 1);
                        pass++;
                    }
                }

                float r1 = ((float)rnd.NextDouble()) - .5f;
                float r2 = ((float)rnd.NextDouble()) - .5f;
                sample.imageX = scrX + r1;
                sample.imageY = scrY + r2;
                //sample.imageY += screenStartLine;
                //sample.Init(this, pass,valuesPerSample, false);
            }
            else
            {
                // In order to update the screen faster for the first 16 passes
                for (; ; )
                {
                    int stepX = pass % 4;
                    int stepY = (pass / 4) % 4;

                    scrX = currentSampleScreenX * 4 + stepX;
                    scrY = currentSampleScreenY * 4 + stepY;

                    currentSampleScreenX++;
                    if (currentSampleScreenX * 4 >= screenWidth)
                    {
                        currentSampleScreenX = 0;
                        currentSampleScreenY++;

                        if (currentSampleScreenY * 4 >= screenHeight)
                        {
                            currentSampleScreenY = Math.Min(0, screenStartLine - 1);
                            pass++;
                        }
                    }

                    // Check if we are inside the screen
                    if ((scrX < screenWidth) && (scrY < screenHeight))
                    {
                        // Ok, it is a valid sample
                        break;
                    }
                    else if (pass >= PreviewPasses)
                    {
                        sample = this.GetSample(prevSample);
                        return sample;
                    }
                }

                float r1 = ((float)rnd.NextDouble()) - .5f;
                float r2 = ((float)rnd.NextDouble()) - .5f;

                sample.imageX = scrX + r1;
                sample.imageY = scrY + r2;
                //sample.imageY += screenStartLine;
                //sample.Init(this, pass, valuesPerSample, false);
            }
            return sample;
        }


        #region ISampler Members


        public float GetLazyValue(Sample sample)
        {
            return rnd.NextFloat();
        }

        public float GetLDValue(Sample sample)
        {
            return rnd.NextFloat();

        }

        public void Init(int width, int height)
        {
            rnd.Reinitialise((int)DateTime.Now.Ticks);
            if (screenWidth == 0 && screenHeight == 0)
            {
                Width = screenWidth = width;
                Height = screenHeight = height;
                screenWidth = width - 1;
                screenHeight = height - 1;
            }
            screenStartLine = Math.Max(0, screenStartLine - 1);
            currentSubSampleIndex = 0;
            pass = 0;
            TotalSamples = 0;
        }

        public int GetPass()
        {
            return pass;
        }

        public long TotalSamples { get; set; }
        public float GetLazyValue(Sample sample, int index)
        {
            return sample.sampleData[unchecked(index)];
        }


        #endregion

        public void Reinitialise(int seed)
        {
            this.rnd.Reinitialise(seed);
        }

        public float NextFloat()
        {
            return rnd.NextFloat();
        }
    }
}